package com.project.controller.web;

import com.project.myBeans.R;
import io.swagger.annotations.Api;
import org.apache.commons.io.FileUtils;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * @Author: TateBrown
 * @date: 2018/11/1 20:00
 * @param:
 * @return:
 */
@EnableAutoConfiguration
@RestController
@RequestMapping(value = "/file")
@Api(description = "文件相关接口",value = "file")
public class FileController{
    /**
     * @Description:题库导入模板下载
     * @param:
     * @return:
     * @author: TateBrown
     * @Date: 2018/11/1
     */
    @RequestMapping("/DownloadproblemTemp")
     public ResponseEntity<byte[]> problemTemp(HttpServletRequest request){
         String url="src/main/resources/题目导入模板.xlsx";
         File file=new File(url);
         try{
             byte[] data = FileUtils.readFileToByteArray(file);
             HttpHeaders headers = new HttpHeaders();
             String filename = new String("题目导入模板.xlsx".getBytes("UTF-8"), "iso-8859-1");
             headers.setContentDispositionFormData("attachment", filename);
             headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);//设置MIME类型 以下载方式
             HttpStatus statusCode = HttpStatus.OK;
             return new ResponseEntity<byte[]>(data, headers, statusCode);
         }catch (IOException e) {
             e.printStackTrace();
             return null;
         }
     }

}
